package com.jmh.util;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AspectjAdvice {

    @Pointcut("execution(* com.jmh.controller.*.*(..))")
    public void myPoint(){}

    @Before("myPoint()")
    public void beforeAdvice() {
        System.out.println("AspectjAdvice.beforeAdvice()----前置通知");
    }

    @After("myPoint()")
    public void afterAdvice() {
        System.out.println("enclosing_type.enclosing_method()----后置通知");
    }

    @AfterReturning("myPoint()")
    public void afterAdvice2() {
        System.out.println("enclosing_type.enclosing_method()----后置返回通知");
    }

    @AfterThrowing("myPoint()")
    public void throwAdvice() {
        System.out.println("AspectjAdvice.throwAdvice()-----异常通知-");
    }

    @Around("myPoint()")
    public Object arroundAdvice(ProceedingJoinPoint p) throws Throwable {
        System.out.println("AspectjAdvice.arroundAdvice()----环绕前置通知");
        String name = p.getSignature().getName();
        System.out.println("调用的方法名["+name+"]");
        Object[] args = p.getArgs();
        for (Object o : args) {
            System.out.println("参数为="+o);
        }
        Object res = p.proceed();
        System.out.println("返回值为="+res);
        System.out.println("AspectjAdvice.arroundAdvice()----环绕后置通知");
        return res;
    }
}
